package com.bw.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.excel.EasyExcel;
import com.bw.config.R;
import com.bw.config.UploadMinioUtils;
import com.bw.domain.Address;
import com.bw.domain.SysUser;
import com.bw.service.AddressService;
import com.bw.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private AddressService addressService;


    private final String salt = "a";

    //列表
    @RequestMapping("list")
    public R list(@RequestBody SysUser user, Integer pageNum, Integer pageSize) {
      //处理时间
        String[] createTimes = user.getCreateTimes();
        if(createTimes!=null && createTimes.length>0){
            user.setStartTime(createTimes[0]);
            user.setEndTime(createTimes[1]);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> list = sysUserService.lists(user);
        PageInfo<SysUser> info = new PageInfo<>(list);
        return R.successed(info);
    }

    //添加
    @RequestMapping("add")
    public R add(@RequestBody SysUser user) {
        String password = user.getPassword();
        String pwd = DigestUtil.md5Hex(password + salt);
        user.setPassword(pwd);
        boolean add = sysUserService.save(user);
        return R.successed(add);
    }

    //物理删除
    @RequestMapping("delete")
    public R delete(Integer id) {
        boolean delete = sysUserService.delete(id);
        return R.successed(delete);
    }

    //修改
    @RequestMapping("update")
    public R update(@RequestBody SysUser user) {
        boolean update = sysUserService.update(user);
        return R.successed(update);
    }

    //导出
    @RequestMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        String fileName = URLEncoder.encode("用户信息", "UTF-8").replaceAll("\\+","%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.opnmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<SysUser> list = sysUserService.list();
        EasyExcel.write(response.getOutputStream(), SysUser.class).sheet("用户信息").doWrite(list);
    }
    //导入
    @RequestMapping("import")
    public Map<String,Object>selectUser(MultipartFile file){
        HashMap<String, Object> map = new HashMap<>();
        try {
            EasyExcel.read(file.getInputStream(), SysUser.class, new CellDataDemoHeadDataListener1(sysUserService)).sheet().doRead();
            map.put("msg","成功");
        } catch (IOException e) {
           e.printStackTrace();
            map.put("msg","失败");
        }
        return map;
    }

    //上传
    @PostMapping("/upload")
    public String upload(MultipartFile file){
        String s = UploadMinioUtils.uploadPic(file);
        System.out.println(file);
        return s;
    }
    //三级联动
    @RequestMapping("getAddress")
    public R getAddres(Integer id){
       List<Address>addressById= this.addressService.GetAddressById(id);
        return R.successed(addressById);
    }
    //报表
    @RequestMapping("gettu")
    public List<Map<String,Object>> zxt(){
        List<Map<String,Object>>list=sysUserService.zList();
        return list;
    }
}
